package ipdesk.model.report.brand.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import ipdesk.model.report.brand.bean.BrandInfoBean;

import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.template.SqlMapDaoTemplate;
import com.opensymphony.xwork2.ActionContext;

import ecg.util.SessionUtils;
import ecg.util.SqlMapLocator;

public class BrandInfoImpl extends SqlMapDaoTemplate implements IBrandInfo
{
	
	public BrandInfoImpl(DaoManager manager)
	{
		super(manager);
	}
	
	public int brandInfoReg(BrandInfoBean brandInfoBean) throws Exception
	{
		int brandCodeCount = 0;
		int lastBrandSeq = 0;
		String brandCode = "";
		
			
		String consultingTime = brandInfoBean.getConsultingDate();
		String deskCode = brandInfoBean.getDeskCode();
		System.out.println("consultTime : " + consultingTime);
		System.out.println("deskCode : " + deskCode);
		// 기업코드를 생성하기 위해 등록날짜와 데스크 코드를 조건으로 한 데이터의 갯수를 가지고온다.
		//brandCodeCount = ((Integer)this.queryForObject("brand.brandCode-count", brandInfoBean)).intValue();
				
		//String endNumber = StringUtils.leftPad(Integer.toString(brandCodeCount+1), 4, "0");
		
		// 생성된 상표출원일지 관리코드.
		//brandCode = consultingTime.replaceAll("-","") + "-" + deskCode.substring(4) + "-BRD-" + endNumber;
		System.out.println(brandCode);
		
		// 생성된 기업관리코드 빈 객체에 저장.
		brandInfoBean.setManageNo(brandCode);
		brandInfoBean.setUserSeq(Integer.parseInt(SessionUtils.getLoginSession().get("userSeq")));
		
		// iBatis 연동. 데이터 insert.
		lastBrandSeq = ((Integer)this.insert("brand.brandInfoInsert", brandInfoBean)).intValue();
		
		return lastBrandSeq;
	}
	
	public void insertFileData(BrandInfoBean brandInfoBean)
	{
		// iBatis 연동. 데이터 insert.
		this.insert("brand.brandFileInfoInsert", brandInfoBean);
	}
	
	
	public int getDataTotalCount(BrandInfoBean brandInfoBean) throws Exception
	{
		System.out.println(" :::: "+SessionUtils.getLoginSession().get("authCode"));

		
		System.out.println("totalCount ::: deskCode :: "+brandInfoBean.getDeskCode());
		Map map = new HashMap();
		map.put("userSeq", SessionUtils.getLoginSession().get("userSeq"));
		map.put("authCode", SessionUtils.getLoginSession().get("authCode"));
		//if(brandInfoBean.getDeskCode() == null || "".equals(brandInfoBean.getDeskCode())){
		//	map.put("deskCode", SessionUtils.getLoginSession().get("deskCode"));
		//}else{
			map.put("deskCode", brandInfoBean.getDeskCode());
		//}
		
		if(brandInfoBean.getBrandSearchKeyword() != null || !"".equals(brandInfoBean.getBrandSearchKeyword())){
			map.put("brandSearchKeyword", brandInfoBean.getBrandSearchKeyword());
			map.put("brandSearchOption", brandInfoBean.getBrandSearchOption());
		}
		
		if(brandInfoBean.getYear() != null || !"".equals(brandInfoBean.getYear())) {
			map.put("year", brandInfoBean.getYear());
		}
		
		if(brandInfoBean.getState() != null || !"".equals(brandInfoBean.getState())) {
			map.put("state", brandInfoBean.getState());
		}
		
		//brandInfoBean.setDeskCode(SessionUtils.getLoginSession().get("deskCode"));	


		int totalDataCount = ((Integer)this.queryForObject("brand.brandInfoTotalcount", map)).intValue();

		return totalDataCount;
	}
	
	public List<BrandInfoBean> getPagingDataListForUser(Map map) throws Exception
	{
		map.put("userSeq", SessionUtils.getLoginSession().get("userSeq"));
		map.put("authCode", SessionUtils.getLoginSession().get("authCode"));
		System.out.println(map.get("startRow"));
		System.out.println(map.get("limitRowCount"));
		List<BrandInfoBean> pageDataList = this.queryForList("brand.brandPageDataListForUser", map);
		
		return pageDataList;
	}
	
	public BrandInfoBean getDetailData(int brandSeq){
		BrandInfoBean brandInfoBean = (BrandInfoBean)this.queryForObject("brand.brandDetailData", brandSeq);
		return brandInfoBean;
	}
	
	public BrandInfoBean getFileDetailData(BrandInfoBean brandInfoBean) {
		// TODO Auto-generated method stub
		return (BrandInfoBean)this.queryForObject("brand.fileDetailData", brandInfoBean);
	}
	
	public void brandInfoUpdate(BrandInfoBean brandInfoBean)
	{
		this.update("brand.brandInfoUpdate", brandInfoBean);
	}
	
	public void insertFileData2(BrandInfoBean brandInfoBean)
	{
		// iBatis 연동. 데이터 insert.
		this.insert("brand.brandFileInfoInsert2", brandInfoBean);
	}
	
	public void delFileData(String fileSeq)
	{
		this.delete("brand.brandDelFileData", fileSeq);
	}
	
	public void delDetailData(int brandSeq)
	{
		this.update("brand.brandDelDetailData", brandSeq);
	}

	public void insertCompleteData(BrandInfoBean brandInfoBean) {
		System.out.println("*바로전*");
		System.out.println(brandInfoBean.getAgentCompanySeq());
		this.update("brand.brandCompleteInsert", brandInfoBean);
	}

	public void delCompleteData(int brandSeq) {
		// TODO Auto-generated method stub
		this.update("brand.brandCompleteDelete", brandSeq);
	}
}
